package guowenlong.golf.ui.personal.info;

import android.app.AlertDialog.Builder;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import guowenlong.golf.R;
import guowenlong.golf.widget.WheelView;
import guowenlong.golf.widget.WheelView.OnWheelViewListener;
import java.util.Arrays;
import java.util.Calendar;

/**
 * des   : 描述 author: Administrator email : guowenlong20000@gmail.com time  : 2017年02月13日 上午 11:41.
 */

public class PersonalInfomationActivity extends AppCompatActivity {

  @BindView(R.id.ib_back)
  TextView ibBack;
  @BindView(R.id.btn_qr)
  Button btnQr;
  @BindView(R.id.ll_userhead)
  LinearLayout llUserhead;
  @BindView(R.id.ll_name)
  LinearLayout llName;
  @BindView(R.id.tv_personal_name)
  TextView tvPersonalName;
  @BindView(R.id.ll_birthday)
  LinearLayout llBirthday;
  @BindView(R.id.ll_sex)
  LinearLayout llSex;
  @BindView(R.id.tv_personal_birthday)
  TextView tvPersonalBirthday;
  @BindView(R.id.tv_personal_sex)
  TextView tvPersonalSex;
  private String sex;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    restoreActionBar();
    setContentView(R.layout.activity_personal_inifomation);
    ButterKnife.bind(this);
  }

  public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.hide();
    actionBar.setDisplayShowTitleEnabled(false);
  }


  @OnClick({R.id.ib_back, R.id.btn_qr, R.id.ll_userhead, R.id.ll_name, R.id.ll_birthday,
      R.id.ll_sex})
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.ib_back:
        finish();
        break;
      case R.id.btn_qr:
        break;
      case R.id.ll_userhead:
        Intent intent = new Intent(this, ChangeHeadImageActivity.class);
        startActivityForResult(intent, 2);

        break;
      case R.id.ll_name:
        Intent nameIntent = new Intent(this, InputTextActivity.class);
        nameIntent.putExtra("title", "修改名字");
        startActivityForResult(nameIntent, 1);
        break;
      case R.id.ll_birthday:
        Calendar c = Calendar.getInstance();
        new DatePickerDialog(this, new OnDateSetListener() {
          @Override
          public void onDateSet(DatePicker view, int year,
              int monthOfYear, int dayOfMonth) {
            tvPersonalBirthday.setText(year + "." + monthOfYear + "." + dayOfMonth);
          }
        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
        break;
      case R.id.ll_sex:
        View outerView = LayoutInflater.from(this).inflate(R.layout.wheel_view, null);
        WheelView wv = (WheelView) outerView.findViewById(R.id.wheel_view_wv);
        wv.setOffset(2);
        wv.setItems(Arrays.asList("男", "女"));
        wv.setSeletion(3);
        wv.setOnWheelViewListener(new OnWheelViewListener() {
          @Override
          public void onSelected(int selectedIndex, String item) {
            sex = item;
          }
        });
        new Builder(this)
            .setTitle("请选择性别")
            .setView(outerView)
            .setPositiveButton("OK", new OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                tvPersonalSex.setText(sex);
              }
            }).show();
        break;
    }
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
      switch (requestCode) {
        case 1:
          tvPersonalName.setText(data.getExtras().getString("name"));
          break;
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}